home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE02 / TPACK / TPACK.ZIP / USERWIN.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-06-01  |  12.0 KB  |  344 lines

  1. {------------------------------------------------------------------------------}
  2. {UNREGISTERED VERSION (6/1/95) PLEASE REDISTRIBUTE IN tPACK.ZIP!
  3.  This revision does not contain everything, nor are the exciting
  4.  DataSetReporter and ExtendedMenu[Item] components included.
  5.  Use SWREG#5906 to receive these, icons and a help file for $130.
  6.  You must register when using this code in a business application!
  7.  You'll receive a license to use this code in up to 50 copies of
  8.  any app you write. In turn you will get responsive e-mail
  9.  tech support and enhancements till I run out of registrations
  10.  or suggestions. Meanwhile.. enjoy the code. Bye! I'll make more.
  11.  {(C)'1995 Michael/Ax-Systems, 71560,1754@Compuserve.com}
  12. {------------------------------------------------------------------------------}
  13.  
  14. unit UserWin;
  15.  
  16. {-----------------------------------------------------------------------------------------}
  17. { USERWIN                                                                                 }
  18. {-----------------------------------------------------------------------------------------}
  19.  
  20. interface
  21.  
  22. uses
  23.   Classes,
  24.   UserInfo;
  25.  
  26. function TrailingChar(Value:String;Trailer:Char):String; {insures a trailing character}
  27. function TrailingBackSlash(Value:String):String;         {insures a trailing '\'}
  28.  
  29. Type
  30.   TWindowsUserInfo = class(TUserInfo)
  31.   {service component to get some windows info as well as unique files that can be automatically
  32.   zapped when the component shuts down. it can also validate a password against the screen saver}
  33.   private
  34.     fUserName,
  35.     fCompanyName,
  36.     fPassWord       : PString;
  37.     fSsDelay        : Integer;
  38.     fZap            : Boolean;
  39.     fUniqueNames    : TStringList;
  40.   protected
  41.     procedure WinEncrypt(Strg: PChar);
  42.     Procedure EncryptCString(S: PChar);
  43.     Function  EncryptString(const S: String): String;
  44.     function GetUserName:String;
  45.     function GetCompanyName:String;
  46.     function GetWindowsPath:String;
  47.     function GetSystemPath:String;
  48.     function GetFreeGDI: integer;
  49.     function GetFreeUser: integer;
  50.     function GetFreeSystem: integer;
  51.     function GetUniqueFileName:String;
  52.     function GetFreeSpace: longint;
  53.     procedure SetNoLongInt(Value:LongInt);
  54.     procedure SetNoInteger(Value:Integer);
  55.     procedure SetNoString(const Value:String);
  56.   public
  57.     Constructor Create(aOwner:TComponent); Override;
  58.     Destructor Destroy; Override;
  59.     function UpdateOK: boolean; Override;
  60.     Function HasPassWord:Boolean;
  61.     Function CheckPassWord(const Value:String):Boolean;
  62.     property UniqueFileName: String read GetUniqueFileName;
  63.   published
  64.     property ZapUniqueOnFree:Boolean read fZap write fZap default true;
  65.     property UserName: String read GetUserName write SetNoString stored false;
  66.     property CompanyName: String read GetCompanyName write SetNoString stored false;
  67.     property SaverDelay: Integer read fssDelay write SetNoInteger stored false;
  68.     property WindowsPath: String read GetWindowsPath write SetNoString stored false;
  69.     property SystemPath: String read GetSystemPath write SetNoString stored false;
  70.     property FreeSpace: Longint read GetFreeSpace write SetNoLongInt stored false;
  71.     property FreeGDI: integer read GetFreeGDI write SetNoInteger stored false;
  72.     property FreeUser: integer read GetFreeUser write SetNoInteger stored false;
  73.     property FreeSystem: integer read GetFreeSystem write SetNoInteger stored false;
  74.     end;
  75.  
  76. implementation
  77.  
  78. uses
  79.   IniFiles
  80.   ,PasUtils
  81.   ,WinTypes
  82.   ,WinProcs
  83.   ,Controls
  84.   ,SysUtils;
  85.  
  86. const
  87.   BufSize = 144;
  88.  
  89. {------------------------------------------------------------------------------}
  90. { TRAILING CHARACTER, TRAILING BACKSLASH                                       }
  91. {------------------------------------------------------------------------------}
  92. {need to include a StringServices component perhaps} {for now these utils are here.}
  93.  
  94. function TrailingChar(Value:String;Trailer:Char):String; {insures a trailing character}
  95. begin
  96.   Result:=Value;
  97.   if copy(Value,length(Value),1)<>Trailer then
  98.     Result:=Result+Trailer;
  99. end;
  100.  
  101. function TrailingBackSlash(Value:String):String; {insures a trailing '\'}
  102. begin
  103.   if Value<>'' then
  104.     Result:=TrailingChar(Value,'\')
  105.   else
  106.     Result:=Value;
  107. end;
  108.  
  109. {-----------------------------------------------------------------------------------------}
  110. { OBJECT CREATION                                                                         }
  111. {-----------------------------------------------------------------------------------------}
  112.  
  113. Constructor TWindowsUserInfo.Create(aOwner:TComponent);
  114. begin
  115.   inherited Create(aOwner);
  116. {  Options:=[uifUpdateOnLoad,uifUpdateOnGet];  }
  117.   fUserName:=NullStr;
  118.   fCompanyName:=NullStr;
  119.   fPassWord:=NullStr;
  120.   fUniqueNames:=TStringList.Create;
  121.   fZap:=True;
  122. end;
  123.  
  124. Destructor TWindowsUserInfo.Destroy;
  125. var
  126.   i,n:longint;
  127. begin
  128.   with fUniqueNames do begin
  129.     n:=Count-1;
  130.     if fZap and (n>-1) then
  131.       for i:=0 to n do
  132.         if FileExists(Strings[i]) then
  133.           DeleteFile(Strings[i]);
  134.     Free;
  135.     end;
  136.   DisposeStr(fUserName);
  137.   DisposeStr(fCompanyName);
  138.   DisposeStr(fPassWord);
  139.   inherited Destroy;
  140. end;
  141.  
  142. function TWindowsUserInfo.UpdateOK: boolean;
  143. var
  144.   Ini:TIniFile;
  145.   fileHandle: THandle;
  146.   zStr:PChar;
  147. begin
  148.   Result:=inherited UpdateOK;
  149.   if not Result then
  150.     Exit;
  151.   Ini := TIniFile.Create('CONTROL.INI');                         { Open the Ini File }
  152.   AssignStr(fPassword,Ini.ReadString('ScreenSaver','Password',''));{ Read the Password }
  153.   Ini.Free;                                                      { Close It }
  154.   SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT,0,@fSsDelay,0);  { Read the Delay }
  155.   if fSsDelay > 0 then fSsDelay := fSsDelay Div 60;              { Get Minutes }
  156.   if fSsDelay = 0 then fSsDelay := 1;                            { JIC an awkward Number }
  157.   { Get user name and company name }                               {what did he mean there?}
  158.   fileHandle := LoadLibrary('USER');
  159.   if fileHandle >= HINSTANCE_ERROR then begin
  160.     zStr:=MakePChar('');
  161.     If LoadString(fileHandle, 514, zStr, 255) <> 0 Then
  162.       AssignStr(fUserName,StrPas(zStr));
  163.     If LoadString(fileHandle, 515, zStr, 255) <> 0 Then
  164.       AssignStr(fCompanyName,StrPas(zStr));
  165.     FreeLibrary(fileHandle);
  166.     end;
  167. end;
  168.  
  169. {-----------------------------------------------------------------------------------------}
  170. { OBJECT PLUMBING                                                                         }
  171. {-----------------------------------------------------------------------------------------}
  172.  
  173. procedure TWindowsUserInfo.SetNoLongInt(Value:LongInt);
  174. begin
  175. end;
  176.  
  177. procedure TWindowsUserInfo.SetNoInteger(Value:Integer);
  178. begin
  179. end;
  180.  
  181. procedure TWindowsUserInfo.SetNoString(const Value:String);
  182. begin
  183. end;
  184.  
  185. function TWindowsUserInfo.GetWindowsPath:String;
  186. var
  187.   Buffer: PChar;
  188.   Count: Word;
  189. begin
  190.   GetMem(Buffer, BufSize);
  191.   Count:=GetWindowsDirectory(Buffer,BufSize);
  192.   Result:=strpas(Buffer);
  193.   FreeMem(Buffer, BufSize);
  194.   Result:=TrailingBackSlash(Result);
  195. end;
  196.  
  197. function TWindowsUserInfo.GetSystemPath:String;
  198. var
  199.   Buffer: PChar;
  200.   Count: Word;
  201. begin
  202.   GetMem(Buffer, BufSize);
  203.   Count:=GetSystemDirectory(Buffer,BufSize);
  204.   Result:=strpas(Buffer);
  205.   FreeMem(Buffer, BufSize);
  206.   Result:=TrailingBackSlash(Result);
  207. end;
  208.  
  209. function TWindowsUserInfo.GetUserName:String;
  210. begin
  211.   Result:=fUserName^;
  212. end;
  213.  
  214. function TWindowsUserInfo.GetCompanyName:String;
  215. begin
  216.   Result:=fCompanyName^;
  217. end;
  218.  
  219. function TWindowsUserInfo.GetFreeSpace: longint;
  220. begin
  221.   Result:=WinProcs.GetFreeSpace(0);
  222. end;
  223.  
  224.  
  225. function TWindowsUserInfo.GetFreeGDI: integer;
  226. begin
  227.   Result:=GetFreeSystemResources(GFSR_GdiResources);
  228. end;
  229.  
  230.  
  231. function TWindowsUserInfo.GetFreeUser: integer;
  232. begin
  233.   Result:=GetFreeSystemResources(GFSR_UserResources);
  234. end;
  235.  
  236.  
  237. function TWindowsUserInfo.GetFreeSystem: integer;
  238. begin
  239.   Result:=GetFreeSystemResources(GFSR_SystemResources);
  240. end;
  241.  
  242.  
  243. {-----------------------------------------------------------------------------------------}
  244. { OBJECT FUNCTIONS                                                                        }
  245. {-----------------------------------------------------------------------------------------}
  246.  
  247. Function TWindowsUserInfo.HasPassWord:Boolean;
  248. begin
  249.   Result:=fPassword^[0]>#0;
  250. end;
  251.  
  252. Function TWindowsUserInfo.CheckPassWord(const Value:String):Boolean;
  253. {can't be constant parameter as we use the buffer to do work with}
  254. var
  255.   Cursor:TCursor;
  256. begin
  257.   if HasPassWord then
  258.     Result:= EncryptString(UpperCase(Value))=fPassWord^
  259.   else
  260.     Result:=True;
  261. end;
  262.  
  263. function TWindowsUserInfo.GetUniqueFileName:String;
  264. {this creates a file!}
  265. {could/should add names to list and delete files on free}
  266. var
  267.   Buffer: PChar;
  268.   Count: Word;
  269. begin
  270.   GetMem(Buffer, BufSize);
  271.   Count:=GetTempFileName(#0,nil,0,Buffer);
  272.   Result:=strpas(Buffer);
  273.   FreeMem(Buffer, BufSize);
  274. end;
  275.  
  276. {-----------------------------------------------------------------------------------------}
  277. { WINDOWS SCREENSAVER PASSWORD ENCRYPTION         REPACKAGED I HOPE I DONT GET SUED!      }
  278. {-----------------------------------------------------------------------------------------}
  279.  
  280. procedure TWindowsUserInfo.WinEncrypt(Strg: PChar);
  281. var
  282.   StrgPt, Strglg : Integer;                                { Local Vars }
  283.   TheByte : Byte;                                          { Working Char }
  284.  
  285.   procedure Exor (x1: byte; var x2: byte);
  286.   const  { the last three are '[]=' - not allowed in profile string }
  287.     NotAllowed = [0..$20, $7f..$90, $93..$9f, $3d, $5b, $5d];
  288.   begin
  289.     if not ((x2 xor x1) in NotAllowed) then
  290.       x2 := x2 xor x1;
  291.   end; { Exor }
  292.  
  293. begin
  294.   StrgLg := lstrlen(Strg);                                 { Get String Length }
  295.   if (StrgLg = 0) then exit;                               { empty string => nothing to do }
  296.   AnsiUpper (Strg);                                        { capitalize the string }
  297.  
  298.   for StrgPt := 0 to StrgLg - 1 do begin                   { proceed from left to right }
  299.     TheByte := byte (Strg [StrgPt]);                       { get character to encrypt }
  300.     Exor (StrgLg, TheByte);                                { xor it using string length...}
  301.     if (StrgPt = 0) then                                   { If EOS }
  302.       Exor ($2a, TheByte)                                  {...a constant...}
  303.     else begin
  304.       Exor (StrgPt, TheByte);                              {...actual string pointer...}
  305.       Exor (byte (Strg [StrgPt-1]), TheByte);              {...previous character }
  306.       end;
  307.     Strg [StrgPt] := char (TheByte);                       { store encrypted byte back }
  308.     end; { for };
  309.  
  310.   if (StrgLg > 1) then                                     { no second pass for one-byte-strings }
  311.     for StrgPt := StrgLg-1 downto 0 do begin               { proceed from right to left }
  312.       TheByte := byte (Strg [StrgPt]);                     {  encrypt similar as in first pass }
  313.       Exor (StrgLg, TheByte);                              { xor it using string length...}
  314.       if (StrgPt = StrgLg - 1) then                        { If BOS }
  315.         Exor ($2a, TheByte)                                {...a constant...}
  316.       else begin
  317.         Exor (StrgPt, TheByte);                            {...actual string pointer...}
  318.         Exor (byte (Strg [StrgPt+1]), TheByte);            {...Next character }
  319.         end;
  320.       Strg [StrgPt] := char (TheByte);                     { store encrypted byte back }
  321.       end; { for };
  322. end;
  323.  
  324.  
  325. Procedure TWindowsUserInfo.EncryptCString(S : PChar);
  326. Begin
  327.   WinEncrypt(S);
  328. end;
  329.  
  330. Function TWindowsUserInfo.EncryptString(const S : String) : string;
  331. begin
  332.   Result := S;
  333.   if Result[0] < #254 then begin
  334.     Result[Integer(Result[0]) + 1] := Chr(0);
  335.     WinEncrypt(@Result[1]);
  336.     end;
  337. end;
  338.  
  339. {-----------------------------------------------------------------------------------------}
  340. {                                                                                         }
  341. {-----------------------------------------------------------------------------------------}
  342.  
  343. end.
  344.